package com.baomidou.springboot.hystrix;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.springboot.entity.User;
import com.baomidou.springboot.util.RedisUtil;

import com.baomidou.springboot.util.SpringUtils;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;

public class SaveShopInfo2ReidsCacheCommand extends HystrixCommand<Boolean> {

	private User shopInfo;
	
	public SaveShopInfo2ReidsCacheCommand(User shopInfo) {
		super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RedisGroup"))
				.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
						.withExecutionTimeoutInMilliseconds(100)
						.withCircuitBreakerRequestVolumeThreshold(1000)
						.withCircuitBreakerErrorThresholdPercentage(70)
						.withCircuitBreakerSleepWindowInMilliseconds(60 * 1000))
				);  
		this.shopInfo = shopInfo;
	}
	
	@Override
	protected Boolean run() throws Exception {
		RedisUtil redisUtil = (RedisUtil) SpringUtils
				.getBean("redisUtil");
		String key = "shop_info_" + shopInfo.getId();
		redisUtil.set(key, JSONObject.toJSONString(shopInfo),0);
		return true;
	}
	
	@Override
	protected Boolean getFallback() {
		return false;
	}

}
